package com.zt.rongda.bus.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.bus.model.Bus;
import com.zt.rongda.bus.model.BusHistory;
import com.zt.rongda.bus.service.BusInfoSyncService;
import com.zt.rongda.bus.service.BusPositionService;
import com.zt.rongda.bus.service.BusService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import com.zt.util.StringUtil;

@Controller
@RequestMapping(value = "/bus")
public class BusController {

    @Autowired
    private BusService busService;

    @Autowired
    private BusPositionService busPositionService;

    @Autowired
    private BusInfoSyncService busInfoSyncService;

    public static long lastRefreshBusTimes = 0;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(busService.getTable(reqBean));
    }
    
    @RequestMapping(value = "/{busId}")
    @ResponseBody
    public JSONObject getBus(@PathVariable String busId) {
        return Response.response(busService.getById(busId));
    }

    @RequestMapping(value = "/refresh")
    @ResponseBody
    public JSONObject refreshMediaBuses(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        int minite = 1;
        if (System.currentTimeMillis() - lastRefreshBusTimes >= minite * 60 * 1000) {
            lastRefreshBusTimes = System.currentTimeMillis();
            busInfoSyncService.loadBusInfo();
            return Response.responseSuccess();
        } else {
            long i = minite - (System.currentTimeMillis() - lastRefreshBusTimes) / 60 / 1000;
            return Response.responseError("请在" + i + "分钟后执行此操作");
        }
    }


    /**
     * 广告历史
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/adHistory")
    @ResponseBody
    public JSONObject adHistory(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(busService.adHistory(reqBean.getInfo("busId")));
    }


    /**
     * 查询车辆异动信息
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/transQuery")
    @ResponseBody
    public JSONObject transQuery(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(busService.transQuery(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        busService.add(getBus(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        busService.update(getBus(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        busService.delete(reqBean.getInfo("busId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/addBusHistory")
    @ResponseBody

    public JSONObject addBusHistory(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        busService.addBusHistory(getNewBus(reqBean), getBusHistory(reqBean));
        return Response.responseSuccess();
    }

    /**
     * 车辆gps数据
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/gps")
    @ResponseBody
    public JSONObject getBusGPS(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(busPositionService.getLastBusPosition(reqBean.getInfo("vehicleNo"),
                reqBean.getInfo("lineNo")));// 自编号 线路号
    }


    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Bus getBus(ReqBean reqBean) {
        Bus bus = new Bus();
        bus.setBusId(reqBean.getInfo("busId"));
        bus.setVehicleNo(reqBean.getInfo("vehicleNo"));
        bus.setVehicleNoAbbr(reqBean.getInfo("vehicleNoAbbr"));
        bus.setPlateNo(reqBean.getInfo("plateNo"));
        bus.setLineNo(reqBean.getInfo("lineNo"));
        bus.setCompanyName(reqBean.getInfo("companyName"));
        bus.setBusType(reqBean.getInfo("busType"));
        bus.setBusMakeType(reqBean.getInfo("busMakeType"));
        bus.setYear(reqBean.getInfo("year"));
        bus.setStopPlace(reqBean.getInfo("stopPlace"));
        bus.setStatus(reqBean.getInfo("status"));
        bus.setOperator(reqBean.getUserId());
        bus.setRemark(reqBean.getInfo("remark"));
        return bus;
    }


    private Bus getNewBus(ReqBean reqBean) {

        String busHistoryStr = reqBean.getInfo("busHistory");
        if(StringUtil.isEmpty(busHistoryStr) || "{}".equals(busHistoryStr)) {
            throw new BusinessException("非法请求!");
        }
        JSONObject json = JSONObject.fromObject(busHistoryStr);
        Bus bus = new Bus();
        bus.setBusId(json.getString("busId"));
        bus.setVehicleNo(json.getString("vehicleNo"));
        bus.setVehicleNoAbbr(json.getString("vehicleNoAbbr"));
        bus.setPlateNo(json.getString("plateNo"));
        bus.setLineNo(json.getString("lineNo"));
        bus.setCompanyName(json.getString("companyName"));
        bus.setBusType(json.optString("busType"));
        bus.setBusMakeType(json.optString("busMakeType"));
        bus.setYear(json.optString("year"));
        bus.setStopPlace(json.optString("stopPlace"));
        bus.setStatus(json.getString("status"));
        bus.setOperator(reqBean.getUserId());
        bus.setRemark(json.optString("remark"));
        return bus;
    }

    private BusHistory getBusHistory(ReqBean reqBean) {
        String originalStr = reqBean.getInfo("original");
        if(StringUtil.isEmpty(originalStr) || "{}".equals(originalStr)) {
            throw new BusinessException("非法请求!");
        }
        JSONObject json = JSONObject.fromObject(originalStr);

        BusHistory busHistory = new BusHistory();
        busHistory.setId(json.optString("id"));
        busHistory.setBusId(json.getString("busId"));
        busHistory.setVehicleNo(json.getString("vehicleNo"));
        busHistory.setVehicleNoAbbr(json.getString("vehicleNoAbbr"));
        busHistory.setPlateNo(json.getString("plateNo"));
        busHistory.setLineNo(json.getString("lineNo"));
        busHistory.setCompanyName(json.getString("companyName"));
        busHistory.setBusType(json.optString("busType"));
        busHistory.setBusMakeType(json.optString("busMakeType"));
        busHistory.setYear(json.optString("year"));
        busHistory.setStopPlace(json.optString("stopPlace"));
        busHistory.setBusStatus(json.getString("status"));
        busHistory.setOperator(reqBean.getUserId());
        busHistory.setRemark(json.optString("remark"));
        busHistory.setMoveRemark(json.getString("moveRemark"));
        return busHistory;
    }


}